from peewee import *
from shapely import Point
from datetime import datetime
from playhouse.pool import PooledMySQLDatabase
from ..config import db

## 链接数据库
mysql_db = PooledMySQLDatabase(**db)

class BaseModel(Model):
    class Meta:
        database = mysql_db

class Shop(BaseModel):
    id: int = AutoField(primary_key=True)
    name: str = CharField(default='')
    dist_id: int = CharField(default=0)
    state: int = CharField(default=0)
    address: str = CharField(default='')
    doorhead: str = IntegerField(default='')
    location = Field(default=Point(0, 0))
    create_at: int = IntegerField(default=datetime.now().timestamp())  # 创建时间
    update_at: int = IntegerField(default=datetime.now().timestamp())  # 更新时间
    class Meta:
        table_name = 'shop'

class Item(BaseModel):
    id: int = AutoField(primary_key=True)
    name: str = CharField(default='')
    thumb: str = CharField(default='')
    price: int = IntegerField(default=0)
    stock: int = IntegerField(default=0)
    presell: bool = BooleanField(default=False)
    shop_id: int = ForeignKeyField(Shop, to_field='id', backref='items')
    create_at: int = IntegerField(default=datetime.now().timestamp())  # 创建时间
    update_at: int = IntegerField(default=datetime.now().timestamp())  # 更新时间
    class Meta:
        table_name = 'shop_item'

class User(BaseModel):
    id: int = AutoField()
    nickname: str = CharField(default='')
    avatar: str = CharField(default='')
    mobile: str = CharField(default='')
    password: str = CharField(default='')
    create_at: int = IntegerField(default=datetime.now().timestamp())  # 创建时间
    class Meta:
        table_name = 'user'
